<template>
  <div>
    <elm-page-tb>
      <template #top>
        <el-input placeholder="请查询输入内容" size="mini" prefix-icon='el-icon-search' style="width:250px;" v-model="postData.name" clearable></el-input>
        <right>
          <el-button-group>
            <el-button icon="el-icon-search" size="mini" @click="tablebinding()">查询</el-button>
          </el-button-group>
        </right>
      </template>
      <elm-table url="/business/plm/product/GetData" :post-data="postData" :page-size="10" height="100%">
        <el-table-column prop="sys_code_key" label="产品编码" align="center" width="120"></el-table-column>
        <el-table-column prop="name" label="名称" align="center"></el-table-column>
        <el-table-column prop="model" label="型号" align="center"></el-table-column>
        <el-table-column prop="specification" label="规格" align="center"></el-table-column>
        <el-table-column prop="category_mc" label="产品类型" align="center"></el-table-column>
        <el-table-column prop="description" label="产品说明" align="center"></el-table-column>
        <el-table-column prop="remark" label="备注" align="center"></el-table-column>
        <el-table-column fixed="right" label="工序" width="80px" align="center">
          <template v-slot="scope">
            <elm-open-button url="craft" title="工序查看" width="60%" height="60%" :data="{ pd_code: scope.row.sys_code_key,is_show:true }" type="text">工序查看</elm-open-button>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="60px" align="center">
          <template v-slot="scope">
            <el-button type="text" size="mini" @click="selectData(scope.row)">选择</el-button>
          </template>
        </el-table-column>
      </elm-table>
    </elm-page-tb>
  </div>
</template>

<script>
export default {
  extends: elmVueBase,
  data() {
    return {
      postData: { name: null},
      plan_code:elm.dialog.request.plan_code
    }
  },
  async created() {
  },
  methods: {
    async selectData(data) {
      data.pid=0;
      data.id=elm.newGuid();
      let r = await elm.post("/business/plm/plan/task/GetTree",{plan_code:this.plan_code});
      if(r.length>0)
        await this.upload(data,data.sys_code_key,r[r.length-1].sort+1);
      else
        await this.upload(data,data.sys_code_key,0);
      let res=await elm.post("/business/plm/product/craft/GetTree",{pd_code:data.sys_code_key});
      console.log(res);
      if(res.length>0)
        this.changePid(res,data.id);
      this.$nextTick(() => {
        this.v('保存成功。')
        this.close();
        this.request.callVue.treebinding();
      });
    },
    changePid(nodes,pid){
      nodes.forEach(async (node,index) => {
        let craft=node.id;
        node.pid=pid;
        node.id=elm.newGuid();
        await this.upload(node,node.pd_code,index,craft);
        if(Array.isArray(node.children)){
          this.changePid(node.children,node.id);
        }
      });
    },
    async upload(data,pd_code,sort,craft){
      let item={
        name:data.name,
        id:data.id,
        pid:data.pid,
        priority:'低',
        status:'待开始',
        pd_code:pd_code,
        plan_code:this.plan_code,
        sort:sort
      };
      if(craft)item.craft=craft;
      console.log(item);
      await elm.post("/business/plm/plan/task/Upsert",item);
    }
  }
}
</script>
